﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using BEGDC.CC.Web.UserControls;
using BEGDC.CC.BL.CCFlow.CR;
using BEGDC.CC.Entity;
using BEGDC.CC.BL.Workflow;
using BEGDC.CC.BL.Common;
using BEGDC.Utility.Castle;

namespace BEGDC.CC.Web.Common
{
    /// <summary>
    /// Summary description for HotelBookingInput.
    /// </summary>
    public class HotelBookingInput : System.Web.UI.Page
    {
        #region Variables
        protected System.Web.UI.WebControls.DropDownList Dp_HotelPreference;
        protected System.Web.UI.WebControls.Button Btn_Save;
        protected System.Web.UI.WebControls.Button Btn_Delete;
        protected System.Web.UI.WebControls.DropDownList Dp_RoomType;
        protected System.Web.UI.WebControls.TextBox Ctl_RoomNumber;
        protected System.Web.UI.WebControls.Button Btn_Close;
        string FormType = "";
        protected System.Web.UI.WebControls.TextBox Ctl_HotelName;
        string AppType = "";
        #endregion

        private void Page_Load(object sender, System.EventArgs e)
        {
            string RowIDStr = Request["RowID"];
            string StateStr = Request["State"];
            FormType = Request["FormType"];
            AppType = Request["AppType"];

            if (RowIDStr != null)
            {
                this.RowID = Convert.ToInt32(RowIDStr);
            }
            else
            {
                this.RowID = 0;
            }
            this.State = StateStr;

            if (!this.IsPostBack)
            {
                this.Btn_Save.Attributes["onclick"] = "javascript:OnSave();";
                this.Btn_Delete.Attributes["onclick"] = "javascript:OnDelete();";
                this.Btn_Close.Attributes["onclick"] = "javascript:OnClose();";
                //this.Btn_Save.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path + "/images/bbw_save.gif";
                //this.Btn_Delete.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path + "/images/bbw_delete.gif";
                //this.Btn_Close.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path + "/images/bbw_close.gif";
                if (AppType == WorkflowTypes.CO)
                {
                    this.Dp_HotelPreference.Visible = false;
                }
                else
                {
                    this.Ctl_HotelName.Visible = false;
                    WebUtility.BuildDropDown(this.Dp_HotelPreference, BEGDC.CC.BL.Common.DicType.Hotels, false, false);
                }

                WebUtility.BuildDropDown(this.Dp_RoomType, BEGDC.CC.BL.Common.DicType.RoomTypes, false, false);

                if (this.RowID >= 0 && State == "Edit")
                {
                    this.Btn_Delete.Visible = true;
                    InitForm();
                }
            }
        }

        #region Display Car Information
        private void DisplayHotelInfo()
        {
            if (this.Dp_RoomType.SelectedItem != null)
            {
                //				int TypeID = Convert.ToInt32(this.Dp_RoomType.SelectedValue);
                //				ICR CRService = AppContainer.Instance[ServiceConst.CRService] as ICR;
                //				BEGDC.CC.Entity.HotelRoomType RmTy = CRService.GetHotelRoomType(TypeID);
                //this.Lb_Price.Text = RmTy.Price.ToString();
            }
        }
        #endregion

        #region RowID State
        protected int RowID
        {
            get { return (int)this.ViewState["HotelRowID"]; }
            set { this.ViewState["HotelRowID"] = value; }
        }
        protected string State
        {
            get { return this.ViewState["HotelState"].ToString(); }
            set { this.ViewState["HotelState"] = value; }
        }
        #endregion

        #region InitForm
        private void InitForm()
        {
            DataTable HotelDataTable = null;
            DataRow HotelRow = null;
            object SessHotelBooking = null;

            if (this.FormType == BEGDC.CC.BL.Common.SysDicItem.CRFormType.EZVisitForm)
            {
                SessHotelBooking = Session[SessionFlag.HotelSessionEZ];
            }
            else if (this.FormType == BEGDC.CC.BL.Common.SysDicItem.CRFormType.NormalForm)
            {
                SessHotelBooking = Session[SessionFlag.HotelSession];
            }

            if (SessHotelBooking != null)
            {
                HotelDataTable = (DataTable)SessHotelBooking;
            }
            else
            {
                return;
            }

            if (this.RowID >= 0)
            {
                HotelRow = HotelDataTable.Rows[this.RowID];
                try
                {
                    this.Ctl_HotelName.Text = HotelRow["HotelInputName"].ToString();
                }
                catch
                {
                    this.Ctl_HotelName.Text = HotelRow["HotelName"].ToString();
                }
                try
                {
                    this.Dp_HotelPreference.SelectedIndex = Dp_HotelPreference.Items.IndexOf(Dp_HotelPreference.Items.FindByValue(HotelRow["HotelId"].ToString()));
                }
                catch
                { }
                this.Dp_RoomType.SelectedIndex = Dp_RoomType.Items.IndexOf(Dp_RoomType.Items.FindByValue(HotelRow["RoomTypeId"].ToString()));
                this.Ctl_RoomNumber.Text = HotelRow["RoomNumber"].ToString();
            }
        }
        #endregion

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Btn_Save.Click += new EventHandler(Btn_Save_Click);
            this.Btn_Delete.Click += new EventHandler(Btn_Delete_Click);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        #region Update Booking Session
        private void UpdateBookingSession()
        {
            DataTable dataTable = null;
            DataRow dataRow = null;
            object SessHotelBooking = null;
            if (this.FormType == BEGDC.CC.BL.Common.SysDicItem.CRFormType.EZVisitForm)
            {
                SessHotelBooking = Session[SessionFlag.HotelSessionEZ];
            }
            else if (this.FormType == BEGDC.CC.BL.Common.SysDicItem.CRFormType.NormalForm)
            {
                SessHotelBooking = Session[SessionFlag.HotelSession];
            }

            if (SessHotelBooking != null)
            {
                dataTable = (DataTable)SessHotelBooking;
            }
            else
            {
                return;
            }

            if (this.State == "New")
            {
                dataRow = dataTable.NewRow();

                dataRow["Id"] = "0";
                if (AppType == WorkflowTypes.CO)
                {
                    dataRow["HotelInputName"] = this.Ctl_HotelName.Text;

                }
                else
                {

                    dataRow["HotelId"] = this.Dp_HotelPreference.SelectedItem.Value.ToString();
                    dataRow["HotelName"] = this.Dp_HotelPreference.SelectedItem.Text;


                }
                dataRow["RoomTypeId"] = this.Dp_RoomType.SelectedItem.Value.ToString();
                dataRow["RoomTypeName"] = this.Dp_RoomType.SelectedItem.Text;
                dataRow["RoomNumber"] = this.Ctl_RoomNumber.Text;

                dataTable.Rows.Add(dataRow);
            }
            else if (this.State == "Edit" && this.RowID >= 0)
            {
                //dataRow = ObtainBookingFromDataTable(dataTable);
                dataRow = dataTable.Rows[this.RowID];

                if (AppType == WorkflowTypes.CO)
                {
                    dataRow["HotelInputName"] = this.Ctl_HotelName.Text;
                }
                else
                {
                    dataRow["HotelId"] = this.Dp_HotelPreference.SelectedItem.Value.ToString();
                    dataRow["HotelName"] = this.Dp_HotelPreference.SelectedItem.Text;
                }
                dataRow["RoomTypeId"] = this.Dp_RoomType.SelectedItem.Value.ToString();
                dataRow["RoomTypeName"] = this.Dp_RoomType.SelectedItem.Text;
                dataRow["RoomNumber"] = this.Ctl_RoomNumber.Text;
            }
            else if (this.State == "Delete" && this.RowID >= 0)
            {
                dataTable.Rows.RemoveAt(this.RowID);
            }
            else
            {
                return;
            }

            //Session[SessionFlag.HotelSessionEZ] = dataTable;
            if (this.FormType == BEGDC.CC.BL.Common.SysDicItem.CRFormType.EZVisitForm)
            {
                Session[SessionFlag.HotelSessionEZ] = dataTable;
            }
            else if (this.FormType == BEGDC.CC.BL.Common.SysDicItem.CRFormType.NormalForm)
            {
                Session[SessionFlag.HotelSession] = dataTable;
            }
        }
        #endregion

        #region Delete
        void Btn_Delete_Click(object sender, EventArgs e)
        {
            try
            {
                this.State = "Delete";
                UpdateBookingSession();
            }
            catch (Exception exp)
            {
                throw exp;
            }
            Response.Write("<script>window.close();window.returnValue='reload';</script>");
        }
        #endregion

        #region Save
        void Btn_Save_Click(object sender, EventArgs e)
        {
            try
            {
                UpdateBookingSession();
            }
            catch (Exception exp)
            {
                throw exp;
            }
            Response.Write("<script>window.close();window.returnValue='reload';</script>");
        }
        #endregion

        #region HotelIndexChanged
        private void HotelIndexChanged(object sender, System.EventArgs e)
        {
            System.Web.UI.WebControls.ListItem listItem = this.Dp_HotelPreference.SelectedItem;
            if (listItem != null)
            {
                WebUtility.BuildHotelRoomTypes(this.Dp_RoomType, Convert.ToInt32(listItem.Value));
            }
        }
        #endregion

        #region Room Type Select Change
        private void RoomType_SelectChange(object sender, System.EventArgs e)
        {
            DisplayHotelInfo();
        }
        #endregion
    }
}
